package leetcode.剑指offer.后40;

import leetcode.剑指offer.pojo.TreeNode;
import org.junit.Test;

/**
 * @author :songyaozhang
 * @date :Created 2021/8/2 17:25
 * @description :
 */
public class 剑指_55_平衡二叉树 {
    @Test
    public void test() {
        Solution solution = new Solution();
        TreeNode root = new TreeNode(1, null, new TreeNode(2, null, new TreeNode(3)));
        System.out.println(solution.isBalanced(root));
    }

    class Solution {
        public boolean isBalanced(TreeNode root) {
            if (root == null) return true;
            System.out.println(height(root.left) + " " + height(root.right));
            if (Math.abs(height(root.left) - height(root.right)) > 1) return false;
            return isBalanced(root.left) && isBalanced(root.right);
        }

        private int height(TreeNode root) {
            if (root == null) return 0;
            return Math.max(root.left == null ? 0 : height(root.left),
                    root.right == null ? 0 : height(root.right))
                    + 1;
        }
    }


}
